import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';

import 'zz_webview.dart';

class AppWebWidget extends StatefulWidget {
  final String url;
  final WebViewController? controller;

  final String? title;

  /// 网页title改变时回调，当自定义WebViewController 此属性无效
  final Function(String title)? titleCallBack;

  /// 是否显示底部 返回 前进 主页 按钮
  final bool isShowBottomAction;

  /// 是否显示顶部加载进度条
  final bool isShowProgress;
  const AppWebWidget(this.url,
      {super.key,
      this.controller,
      this.title,
      this.titleCallBack,
      this.isShowBottomAction = true,
      this.isShowProgress = true});

  @override
  State<AppWebWidget> createState() => _AppWebWidgetState();

  // static showPrivacyUrl() {
  //   ZZNavigator.push(
  //       widget: AppWebWidget(
  //     Global.privacyUrl,
  //     title: "Privacy Policy".localizable(),
  //     isShowBottomAction: false,
  //   ));
  // }
}

class _AppWebWidgetState extends State<AppWebWidget> {
  String? _title;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text(widget.title ?? _title ?? "")),
      body: ZZWebWidget(
        widget.url,
        controller: widget.controller,
        isShowBottomAction: widget.isShowBottomAction,
        isShowProgress: widget.isShowProgress,
        titleCallBack: (title) {
          if (mounted) {
            _title = title;
            setState(() {});
            widget.titleCallBack != null ? widget.titleCallBack!(title) : null;
          }
        },
      ),
    );
  }
}
